package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.constraintlayout.utils.widget.ImageFilterView
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.CheckListCommonEntity
import com.c.ctgproject.entity.guang.OutCheckEntity
import com.c.ctgproject.ui.collect.MaterialWarehouseOutSubmitActivity
import com.c.ctgproject.ui.collect.check.CreatePassCheckActivity
import com.c.ctgproject.ui.collect.check.OutCheckDetailActivity
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.noDataHandler
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.startActivity

class MyMaterialOutAdapter(private val context: Context, private val mList: ArrayList<OutCheckEntity.DataBean.ListBean>, private val listener:MyMaterialOutAdapterListener) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_my_material_out_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mNo.text = data.bizNo.handlerNull()
//            val dismantleType = when (data.outMethod) {
//                1 -> "电池"
//                2 -> "零件"
//                3 -> "物料"
//                else -> "暂无数据"
//            }
//            holder.dismantleType.text = dismantleType

            val outType = when (data.outType) {
                1 -> "销售出库"
                2 -> "借用出库"
                3 -> "其他出库"
                else -> "暂无数据"
            }
            holder.outType.text = outType


            holder.mTimeTV.text = data.createTime.noDataHandler()
            holder.mUserNameTV.text = data.createPersonName.noDataHandler()
            holder.mBottomRL.setOnClickListener {
                holder.mBTN.performClick()
            }
            //详情
            holder.mParentLL.setOnClickListener {
//                val show = data.status != 1
                context.startActivity<OutCheckDetailActivity>(
                    MyConstants.ID to data.outOrderId,
                    MyConstants.JUST_SHOW to true
                )

            }
            //放行条状态 0-新建(草稿) 1-待审批 2-已完成
            when (data.status) {
                0 -> {
                    holder.mBottomRL.visibility = View.VISIBLE
                    holder.mStatueIV.visibility = View.GONE
                    holder.mBTN.text = "继续编辑"
                }
                1 -> {
                    if (data.isEditable==1){
                        holder.mBottomRL.visibility = View.VISIBLE
                        holder.mStatueIV.visibility = View.GONE
                        holder.mBTN.text = "编辑"
                    }else{
                        //啥都没有
                        holder.mBottomRL.visibility = View.GONE
                        holder.mStatueIV.visibility = View.GONE
                    }
                }
                2 -> {
                    //已完成的展示通过标签
                    holder.mStatueIV.visibility = View.VISIBLE
                    if (data.isReject == 1) {
                        holder.mBTN.text = "修改申请"
                        holder.mStatueIV.imageResource = R.drawable.icon_refuse_right
                        //没通过的可以重新修改
                        holder.mBottomRL.visibility = View.VISIBLE
                    } else {
                        holder.mStatueIV.imageResource = R.drawable.icon_agree_right
                        holder.mBottomRL.visibility = View.GONE
                    }
                }
            }
            holder.mBTN.setOnClickListener {
                when(holder.mBTN.text){
                    "编辑"->{
                        context.startActivity<MaterialWarehouseOutSubmitActivity>(MyConstants.ID to data.outOrderId,MyConstants.UPDATE to true)
                    }

                    "继续编辑"->{
                        context.startActivity<MaterialWarehouseOutSubmitActivity>(MyConstants.ID to data.outOrderId,MyConstants.Draft to true)
                    }
                    "修改申请"->{
                        context.startActivity<MaterialWarehouseOutSubmitActivity>(MyConstants.ID to data.outOrderId )
                    }
                }
            }
            holder.mParentLL.setOnLongClickListener {
                if (data.status==0){
                    //可以删除草稿
                    listener.onItemLongClick(position)
                }
                true
            }
        }
    }
    interface MyMaterialOutAdapterListener{
        fun onItemLongClick(position: Int)
    }
    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mStatueIV: ImageFilterView = view.findViewById(R.id.mStatueIV)
        val outType: TextView = view.findViewById(R.id.outType)
//        val dismantleType: TextView = view.findViewById(R.id.dismantleType)
        val mNo: TextView = view.findViewById(R.id.mNo)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mUserNameTV: TextView = view.findViewById(R.id.mUserNameTV)
        val mBottomRL: RelativeLayout = view.findViewById(R.id.mBottomRL)
        val mBTN: Button = view.findViewById(R.id.mBTN)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
    }

}